<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item label="病区名称" class="w-[200px]">
          <SelectArea v-model:value="formState.bqid" select-first @change="onSelectChange" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="mb-3">
        <a-button type="primary" @click="onClickEdit()">新增</a-button>
        <!-- <a-button @click="onExport">导出</a-button> -->
      </a-space>
      <BaseTable :loading="loading" column-code="00001053" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record.status === 1" color="red">占用</a-tag>
            <a-tag v-else-if="record.status === 2" color="red">停用</a-tag>
            <a-tag v-else color="blue">启用</a-tag>
          </template>
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'isExtraBed'">
            {{ record.isExtraBed ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickEdit(record)"> 修改 </a-button>
            <a-divider type="vertical" />
            <ConfirmModal description="确定删除?" @confirm="onConfirm(record)">
              <a-button type="text" danger size="small"> 删除 </a-button>
            </ConfirmModal>
            <a-divider type="vertical" />
            <a-button v-if="record.status === 2" type="link" size="small" @click="onClickEnable(record, 0)"> 启用 </a-button>
            <ConfirmModal v-if="record.status === 0" description="确定停用?" @confirm="onClickEnable(record, 2)">
              <a-button type="text" danger size="small"> 停用 </a-button>
            </ConfirmModal>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" @confirm="editOpen = false" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import ConfirmModal from '@/components/ConfirmModal'
import SelectArea from '@/components/SelectArea'
import Edit from './components/Edit.vue'

const rowData = ref({})
const editOpen = ref(false)
const formRef = ref(null)
const formState = ref({})
// const wardData = ref([])

const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/opex/getBedInfoList',
  params: formState.value
})
const onClickEdit = (row = {}) => {
  if (Object.keys(row).length !== 0) {
    rowData.value = { ...row }
  } else {
    rowData.value = { wardName: formState.value.wardName, wardId: formState.value.bqid }
  }
  editOpen.value = true
}

const onSelectChange = (wardId, wardName) => {
  formState.value.wardName = wardName
  getData()
}

const onConfirm = async (row) => {
  const { code, msg } = await post('/operation/opex/doDelBedInfo', row)
  code ? message.error(msg) : message.success(msg)
  getData()
}

const onClickEnable = async (row, status) => {
  const { code, msg } = await post('/operation/opex/doModifyBedStatus', { status, id: row.id })
  code ? message.error(msg) : message.success(msg)
  getData()
}

// const onCconDeactivate = async (row) => {
//   const { code, msg } = await post('/operation/opex/doConfirmBedType', { status: row.status, id: row.id })
//   code ? message.error(msg) : message.success(msg)
//   getData()
// }
const onSuceess = () => {
  editOpen.value = false
  getData()
}
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
